import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import connection.ControllerConnectionIn;
import connection.ControllerConnectionOut;
import connection.ControllerMessage;
import connection.Relay;
import connection.SensorMessage;
import controller.Zijde;


public class Controller {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//while (true) {
			try {
				ServerSocket ssckt = new ServerSocket(11145);
				Socket socket = ssckt.accept();
				
				System.out.println("Socket is open...");
				
				Relay relay = new Relay();
				ControllerConnectionIn connIn = new ControllerConnectionIn(socket, relay);
				ControllerConnectionOut connOut = new ControllerConnectionOut(socket, relay);
				Thread connInThread = new Thread(connIn);
				Thread connOutThread = new Thread(connOut);
				connInThread.start();
				connOutThread.start();
				
				System.out.println("Input and Output threads running...");
				
				boolean msgReceived = false;
				while (!msgReceived) {
					SensorMessage msg = relay.getSensorMessage();
					if (msg != null) {
						msgReceived = true;
						System.out.println("SensorMessage received: ");
						System.out.println("Lane: " + msg.getLane());
						System.out.println("Sensor: " + msg.getSensor());
					}
					else {
						try {
							Thread.sleep(100);
						} 
						catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
				
				relay.addControllerMessage(new ControllerMessage(1, 1));
				
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
		//}			
	}	
}
